﻿using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web.Script.Serialization;

namespace ScanSnap.Util
{
    class JsonReader
    {
        public static string JsonSerializer<T>(T t)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ser.WriteObject(ms, t);
            string jsonString = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return jsonString;
        }

        public static T JsonDeserializeString<T>(string jsonString)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            T obj = (T)ser.ReadObject(ms);
            return obj;
        }

        public static T JsonDeserializeUrl<T>(string url)
        {
            using (WebClient client = new WebClient())
            {
                try
                {
                    string jsonString = client.DownloadString(url);
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
                    T obj = (T)ser.ReadObject(ms);
                    return obj;
                }
                catch (WebException)
                {
                    //logger
                    throw new WebException("Cannot connect to server");
                }
            }
        }

        public static T JsonDeserialize<T>(string uri, string method, NameValueCollection nameValues)
        {
            using (WebClient client = new WebClient())
            {
                try
                {
                    byte[] response = client.UploadValues(uri, method, nameValues);
                    string s = Encoding.UTF8.GetString(response, 0, response.Length);
                    Debug.WriteLine("response.Length    " + response.Length + "     ===    " + s);
                    //string jsonString = client.DownloadString(url);
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                    MemoryStream ms = new MemoryStream(response);
                    T obj = (T)ser.ReadObject(ms);
                    return obj;
                }
                catch (WebException)
                {
                    //logger
                    throw new WebException("Cannot connect to server");
                }
                catch (SerializationException)
                {
                    throw new WebException("Invalid response data, try again.");
                }
            }
        }

        public static T JsonDeserialize2<T>(string uri, string method, NameValueCollection nameValues)
        {
            using (WebClient client = new WebClient())
            {
                try
                {
                    Debug.WriteLine("This Is From JsonDeserialize 2   " + uri);
                    byte[] response = client.UploadValues(uri, method, nameValues);

                    
                    string s = Encoding.UTF8.GetString(response, 0, response.Length);
                    string js = s.Substring(1,s.Length - 2);
                    Debug.WriteLine("response.Length    " + response.Length+"     ===    "+js);

                    try
                    {

                        JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                        T obj = json_serializer.Deserialize<T>(js);
                        return obj;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error Occred " + ex.Message);
                        throw new WebException("Cannot connect to server");
                    }

                    //DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                    //MemoryStream ms = new MemoryStream(response);
                    //T obj = (T)ser.ReadObject(ms);
                    
                }
                catch (WebException)
                {
                    //logger
                    throw new WebException("Cannot connect to server");
                }
            }
        }

        public static T JsonDeserialize<T>(string uri, string method)
        {
            using (WebClient client = new WebClient())
            {
                try
                {
                    NameValueCollection nameValues = new NameValueCollection();
                    byte[] response = client.UploadValues(uri, method, nameValues);
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                    MemoryStream ms = new MemoryStream(response);
                    T obj = (T)ser.ReadObject(ms);
                    return obj;
                }
                catch (WebException)
                {
                    //logger
                    throw new WebException("Cannot connect to server");
                }
            }
        }

    }
}
